package com.oocl.ita.util;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class DBUtil {
	private static String oracleUrl = "jdbc:oracle:thin:@ITA-031-W7:1521:xe";
	private static String username = "ZENGNO2";
	private static String password ="ZENGNO2";
	private static final String driverClass = "oracle.jdbc.OracleDriver";

	public static Connection connect() throws SQLException {
		Connection connection = null;
		try {
			Class.forName(driverClass);
			connection = DriverManager.getConnection(oracleUrl, username, password);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return connection;
	}

	public static void free(Connection con, PreparedStatement pst, ResultSet rs) {
		try {
			if (rs != null) {
				rs.close();
			}
			if (pst != null) {
				pst.close();
			}
			if (con != null) {
				con.close();
			}
		} catch (SQLException e) {

		}
	}

	public static void initDBConfig() throws IOException {
		Properties properties = new Properties();
		properties.load(DBUtil.class.getResourceAsStream("/DBConfig.properties"));
//		properties.load(new FileInputStream("DBConfig.properties"));
		oracleUrl = properties.getProperty("oracleUrl");
		username = properties.getProperty("oracleUsername");
		password = properties.getProperty("oraclePassword");
	}
}
